<template>
  <el-dialog width="500px" title="设置充值押金" :visible.sync="dialogVisible" :close-on-click-modal="false">
    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="订单编号">
        <el-input v-model="form.orderId" disabled />
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="form.amount" @input="val => changeMoneyInput(form, 'amount')" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { orderRecharge } from '@/api/order'
export default {
  // props: ['orderId'],
  props: {
    orderId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {
        orderId: '',
        amount: ''
      },
      rules: {
        amount: [{ required: true, message: '金额不能为空', trigger: 'blur' }]
      },
      dialogVisible: false
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.form.orderId = this.orderId
        this.form.amount = ''
      } else {
        this.$refs['ruleForm'].resetFields()
      }
    }
  },
  methods: {
    changeMoneyInput(obj, key) {
      const pattern = /^(([0-9]+)|([0-9]+\.[0-9]{0,2}))$/ // 正数的正则表达式
      // 不符合时
      if (!pattern.test(obj[key])) {
        // input 框绑定的内容为空
        obj[key] = ''
      }
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          orderRecharge({ ...this.form }).then((res) => {
            this.dialogVisible = false
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
